package com.apesay.test;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * ClassName:StartUp
 * Reason:	 
 * Date:     2017/11/1 15:24
 *
 * @author sid
 */
public class StartUp {

    static class DynamicProxy implements InvocationHandler {
        Object oriObj ;
        Object bind(Object oriObj){
            this.oriObj = oriObj ;
            // Proxy 是面向接口的；所有使用Proxy的对象必须有接口的定义
            return Proxy.newProxyInstance(oriObj.getClass().getClassLoader(),oriObj.getClass().getInterfaces(), this) ;
        }
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            doBefore();
            Object invoke = method.invoke(oriObj, args);
            doAfter();
            return  invoke;
        }

        private void doAfter() {
            System.out.println(" do DynamicProxy after ...");
        }

        private void doBefore() {
            System.out.println(" do DynamicProxy before ...");
        }
    }
    
    
    public static void main(String[] args) {
        User user = new User("sid",17);
        user.isAdult();
        user.isChild();
        System.out.println("-------------------normal"+user.getClass().getClassLoader().getClass().getName());
        
        IUser iUser = (IUser)new DynamicProxy().bind(new User("jack",19));
        iUser.isAdult();
        iUser.isChild();
        System.out.println("-------------------proxy"+iUser.getClass().getClassLoader().getClass().getName());
        
    }
}
